import axios from 'axios'

const xlAjax = axios.create({
  baseURL: process.env.NODE_ENV == 'development' ? '' : '',
  headers: {
    post: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  },
  timeout: 30000,
  validateStatus(status) {
    return status < 500
  }
})

const error = (promise) =>
  promise.catch(error => {
    console.log(error)
  })

// user statistics
export const statistics = (api, uid, success) =>
  error(xlAjax.post(api, {
    uid: uid
  }).then(res => {
    success(res)
  }))

// table mixins
export const tableFetch = (vm) => {
  // console.log(vm)
  vm.loading = true
  return error(xlAjax.post(vm.api, {
    uid: vm.uid,
    page: vm.page
  }).then(res => {
    console.log(res)
    vm.loading = false
    vm.tableData = res.data.tableData
    vm.pagination = res.data.pagination
  }).catch(res => {
    vm.$notify({
      message: res
    })
  }))
}

// user detail info
// 获取用户基本信息
export const getUserInfo = vm=>error(xlAjax.post('/user-detail',{
  uid:vm.uid
}))

//user detail 添加回访
export const addCallBack =data =>error(xlAjax.post('/addCallBack',data))